<?php

function unsDepartement_addTeachingUnits($name, $code, $coefficient, $idSemester) {
    global $babDB;
    global $babBody;

    if (null != $name && null != $code && null != $coefficient && null != $idSemester) {

        $babDB->db_query("INSERT INTO " . UNSDEPARTEMENT_TEACHING_UNITS_TABLE_NAME . " VALUES (null, '" . $name . "', '" .$code. "', ".$coefficient. ", " . $idSemester . ", now(), " . $GLOBALS['BAB_SESS_USERID'] . ");");

    } else {
        $babBody->addError(bab_translate("All fields are required!", $GLOBALS['babAddonFolder']));
    }
}

function unsDepartement_deleteTeachingUnit($id) {
    
    $materialsList = unsDepartement_getMaterialsForTeachingUnit($id);

    foreach($materialsList as $material)
    {
        unsDepartement_deleteMaterial($material['id']);
    }
    
    global $babDB;
    
    $babDB->db_query("DELETE FROM " . UNSDEPARTEMENT_TEACHING_UNITS_TABLE_NAME . " WHERE id=".$id);
}

function unsDepartement_getTeachingUnitsForSemester($id) {
    global $babDB;


    $res = $babDB->db_query("select * from " . UNSDEPARTEMENT_TEACHING_UNITS_TABLE_NAME . " where teachingUnit_semesterID = " . $id);

    $teachingUnits = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $teachingUnits[$i] = $arr;
        $i++;
    }
    return $teachingUnits;
}

?>
